package com.example;

import javafx.animation.PauseTransition;
import javafx.scene.control.Label;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.paint.Color;
import javafx.stage.Popup;
import javafx.stage.Window;
import javafx.util.Duration;
import javafx.scene.layout.VBox;

/**
 * 自定义提示弹窗组件
 * @author dc
 */
public class HintPopup {

    /**
     * 显示提示信息
     * @param ownerWindow 父窗口
     * @param message 提示信息
     */
    public static void showPopup(Window ownerWindow, String message) {
        // 创建Popup实例
        Popup popup = new Popup();

        // 创建标签显示文本
        Label messageLabel = new Label(message);
        messageLabel.setStyle("-fx-text-fill: white; -fx-font-size: 18px;");

        // 创建容器
        VBox container = new VBox(messageLabel);
        container.setStyle("-fx-padding: 15px;");
        container.setBackground(new Background(
                new BackgroundFill(Color.rgb(0, 0, 0, 0.8), new CornerRadii(5), null)
        ));

        // 将容器添加到Popup
        popup.getContent().add(container);

        // 计算Popup显示位置（居中）
        popup.setOnShown(event -> {
            double popupWidth = container.getWidth();
            double popupHeight = container.getHeight();

            double ownerWidth = ownerWindow.getWidth();
            double ownerHeight = ownerWindow.getHeight();
            double ownerX = ownerWindow.getX();
            double ownerY = ownerWindow.getY();

            double popupX = ownerX + (ownerWidth - popupWidth) / 2;
            double popupY = ownerY + (ownerHeight - popupHeight) / 2;

            popup.setX(popupX);
            popup.setY(popupY);
        });

        // 显示Popup
        popup.show(ownerWindow);

        // 设置5秒后自动关闭
        PauseTransition delay = new PauseTransition(Duration.seconds(5));
        delay.setOnFinished(event -> popup.hide());
        delay.play();
    }
}
